package com.aoip.telemetry.telemetry.configureui.business.protocol.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.protocol.mapper.ProtocolBaseMapper;
import com.aoip.telemetry.telemetry.configureui.business.protocol.service.ProtocolBaseService;
import com.aoip.telemetry.telemetry.configureui.common.file.service.FileBaseService;
import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.core.util.ArrayTransferUtil;
import com.aoip.telemetry.telemetry.entity.common.FileBaseEntity;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.protocol.ProtocolBaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/2/16 14:22
 */
@Service
public class ProtocolBaseServiceImpl implements ProtocolBaseService {
    @Resource
    private ProtocolBaseMapper mapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    private FileBaseService fileBaseService;

    @Override
    public List<ProtocolBaseEntity> list(ProtocolBaseEntity entity) {
        QueryWrapper<ProtocolBaseEntity> queryWrapper = new QueryWrapper<>();
        String protocolName = entity.getProtocolName();
        if (StringUtils.hasText(protocolName)) {
            queryWrapper.like(true, "protocol_name", protocolName);
        }
        return mapper.selectList(queryWrapper);
    }

    @Override
    public ProtocolBaseEntity protocolBase(String id) {
        ProtocolBaseEntity entity = mapper.selectById(id);
        String protocolPorts = entity.getProtocolPorts();
        int[] ports = ArrayTransferUtil.string2Array(protocolPorts, ",");
        entity.setProtocolPort(ports);
        return entity;
    }

    @Override
    public int insert(ProtocolBaseEntity entity) {
        save(entity);
        return mapper.insert(entity);
    }

    @Override
    public int update(ProtocolBaseEntity entity) {
        save(entity);
        return mapper.updateById(entity);
    }

    private void save(ProtocolBaseEntity entity){
        int[] protocolPort = entity.getProtocolPort();
        String ports = ArrayTransferUtil.array2Int(protocolPort, ",");
        entity.setProtocolPorts(ports);
        String protocolFileId = entity.getProtocolFileId();
        if (StringUtils.hasText(protocolFileId)) {
            FileBaseEntity file = fileBaseService.file(protocolFileId);
            entity.setProtocolFilePath(file.getFilePath());
        }
        redisCache.setCacheObject(CacheKeyEnum.PROTOCOL + ":" + entity.getId(), entity);
    }

    @Override
    public int initCache() {
        List<ProtocolBaseEntity> list = list(new ProtocolBaseEntity());
        Collection<String> keys = redisCache.keys(CacheKeyEnum.PROTOCOL + ":*");
        redisCache.deleteObject(keys);
        list.forEach(item -> {
            String protocolFileId = item.getProtocolFileId();
            if (StringUtils.hasText(protocolFileId)) {
                FileBaseEntity file = fileBaseService.file(protocolFileId);
                item.setProtocolFilePath(file.getFilePath());
            }
            redisCache.setCacheObject(CacheKeyEnum.PROTOCOL + ":" + item.getId(), item);
        });
        return list.size();
    }

    @Override
    public void delete(long[] ids) {
        for (long id : ids) {
            redisCache.deleteObject(CacheKeyEnum.PROTOCOL + ":" + id);
            mapper.deleteById(id);
        }
    }

}
